package com.xs.ahoi.hcxs.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import butterknife.Unbinder;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.CatalogDao;
import com.xs.ahoi.hcxs.model.Catalog;
import com.xs.ahoi.hcxs.utils.ButterKnifeUtil;
import com.xs.ahoi.hcxs.utils.Constants;
import com.xs.ahoi.hcxs.utils.MapUtil;
import com.xs.ahoi.hcxs.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * author: ahoi
 * date: 2018/6/27 17:24
 * description: 目录
 */
public class CatalogFragment extends Fragment {

    @BindView(R.id.lvCatalog)
    public ListView lvCatalog;

    // 文件目录名集合
    private List<Catalog> catalogs;
    private List<Map<String, Object>> datas;
    private CatalogDao catalogDao;
    private FragmentActivity context;
    // 用于ButterKnife解绑
    private Unbinder unbinder;
    private Callback listener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_catalog, container, false);
        unbinder = ButterKnife.bind(this, view);
        context = getActivity();
        init();
        return view;
    }

    private void init() {
        catalogDao = CatalogDao.getInstance();

        // 查询目录
        int bookId = getArguments().getInt(Constants.BOOK_ID);
        catalogs = catalogDao.listByBookId(bookId);

        // 查询当前书籍正在看的章节目录
        Catalog currentOne = catalogDao.getCurrentOneByBookId(bookId);

        // 组装datas数据
        datas = new ArrayList<>();
        Map<String, Object> map;
        Catalog catalog;
        int position = 0;
        for (int i = 0; i < catalogs.size(); i++) {
            catalog = catalogs.get(i);
            map = MapUtil.build().put("title", catalog.getName()).put("isCurrent", 0).over();

            // 标记当前章节
            if (currentOne != null && catalog.getId() == currentOne.getId()) {
                map.put("isCurrent", 1);
                position = i;
            }
            datas.add(map);
        }

        // 绑定adapter
        CatalogFragAdapter adapter = new CatalogFragAdapter(context, datas, R.layout.listview_item_catalog, new String[]{"title"}, new int[]{R.id.tvTitle});
        lvCatalog.setAdapter(adapter);
        lvCatalog.setSelection(position);
    }

    @OnItemClick({R.id.lvCatalog})
    public void onItemClick(int position) {
        // 返回到内容页面的指定下标位置
        Catalog catalog = catalogs.get(position);
        listener.back(catalog.getPosition());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnifeUtil.unbind(unbinder);
    }

    // 自定义SimpleAdapter
    private class CatalogFragAdapter extends SimpleAdapter {

        private CatalogFragAdapter(Context context, List<? extends Map<String, Object>> datas, int resource, String[] from, int[] to) {
            super(context, datas, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            // 将当前章节标记为蓝色
            Map<String, Object> data = getItem(position);
            TextView tvTitle = view.findViewById(R.id.tvTitle);
            if (ObjectUtils.toInteger(data.get("isCurrent")) == 1) {
                tvTitle.setTextColor(ContextCompat.getColor(context, R.color.colorRed));
            } else {
                tvTitle.setTextColor(ContextCompat.getColor(context, R.color.colorBlack));
            }
            return view;
        }

        @Override
        public Map<String, Object> getItem(int position) {
            return datas.get(position);
        }
    }

    public void setListener(Callback listener) {
        this.listener = listener;
    }

    public interface Callback {
        void back(int position);
    }
}
